clear all
capture log close
capture set virtual off
capture set matsize 10000
capture set maxvar 30000

capture set more off
capture virtual off

pause on

/* This file generates the tables in the paper
Change the next line to replicate these results:
Point Stata to wherever you have the data file.
*/
cd "...\analysis_clean"
use main, clear

***************************************
*** Figure 2: Treatment effect ********
***************************************

replace t_minus_treatment= -48 if t_minus_treatment< -48 & treatment==1 
replace t_minus_treatment= 24 if t_minus_treatment > 24  & treatment==1 //freqency after 24 is so low

forvalues l=0/24{
      gen L`l'event=t_minus_treatment==`l'
	  }
forvalues l=48(-1)1{
      gen F`l'event=t_minus_treatment==-`l'
	  }

	drop F1event  // -1 as reference
//---------figure2(a)------------------------
	
preserve

reg log_price_month F*event L*event i.treatment i.month i.firm_code t, robust cluster(firm_code)

matrix coeff = e(b)
matrix v = e(V)
local df_r = e(df_r)

quiet gen coeff_brand = .
quiet gen pval_brand = .

forval tadj = 0/71 {
	quiet gen stat_`tadj'_coeff = .
	quiet gen stat_`tadj'_ci_lower = .
	quiet gen stat_`tadj'_ci_upper = .
}

forval tadj = 0/71 {
	local t = `tadj'+1
	matrix b =e(b)
	matrix v =e(V)
	local df_r = e(df_r)
	local bound_wt = sqrt(v[`t',`t'])*invttail(`df_r',.025)
	replace stat_`tadj'_coeff = b[1,`t']
	replace stat_`tadj'_ci_lower = b[1,`t'] - `bound_wt'
	replace stat_`tadj'_ci_upper = b[1,`t'] + `bound_wt'	
}
contract stat_*

gen i = 1
reshape long @_coeff @_ci_lower @_ci_upper, i(i) j(stat) string
drop i _freq
rename _coeff coeff
rename _ci_lower ci_lower
rename _ci_upper ci_upper
replace stat = subinstr(stat, "stat_", "", .)
rename stat t
destring t, replace
sort t

replace t = t - 48 if t<47
replace t = t - 47 if t>=47

egen x=mean(coeff) if t<0
egen x2=mean(x)
replace coeff=coeff-x2
replace ci_lower=ci_lower-x2
replace ci_upper=ci_upper-x2
insobs 1
replace t=-1 if _n==73
sort t

outsheet t coeff ci_lower ci_upper using "treatment_by_t_price.csv", comma replace
clear

restore
//---------figure2(b)------------------------
preserve

reg log_total_month F*event L*event i.treatment i.month i.firm_code t, robust cluster(firm_code)

matrix coeff = e(b)
matrix v = e(V)
local df_r = e(df_r)

quiet gen coeff_brand = .
quiet gen pval_brand = .

forval tadj = 0/71 {
	quiet gen stat_`tadj'_coeff = .
	quiet gen stat_`tadj'_ci_lower = .
	quiet gen stat_`tadj'_ci_upper = .
}

forval tadj = 0/71 {
	local t = `tadj' + 1
	matrix b =e(b)
	matrix v =e(V)
	local df_r = e(df_r)
	local bound_wt = sqrt(v[`t',`t'])*invttail(`df_r',.025)
	replace stat_`tadj'_coeff = b[1,`t']
	replace stat_`tadj'_ci_lower = b[1,`t'] - `bound_wt'
	replace stat_`tadj'_ci_upper = b[1,`t'] + `bound_wt'	
}

contract stat_*

gen i = 1
reshape long @_coeff @_ci_lower @_ci_upper, i(i) j(stat) string
drop i _freq
rename _coeff coeff
rename _ci_lower ci_lower
rename _ci_upper ci_upper
replace stat = subinstr(stat, "stat_", "", .)
rename stat t
destring t, replace
sort t

replace t = t - 48 if t<47
replace t = t - 47 if t>=47

egen x=mean(coeff) if t<0
egen x2=mean(x)
replace coeff=coeff-x2
replace ci_lower=ci_lower-x2
replace ci_upper=ci_upper-x2
insobs 1
replace t=-1 if _n==73
sort t

outsheet t coeff ci_lower ci_upper using "treatment_by_t_use.csv", comma replace 
clear

restore
